/*
 * @(#)OtherIDTransformer.java
 * Date 2013-08-25
 * Version 1.0
 * Author Jim Horner
 * Copyright (c)2012
 */


package us.hornerscorners.lamppost.hl7v3common.transformer;

import javax.enterprise.context.ApplicationScoped;

import javax.inject.Inject;

import org.apache.commons.lang3.StringUtils;

import org.hl7.v3.COCTMT150002UV01Organization;
import org.hl7.v3.PRPAMT201304UV02OtherIDs;

import us.hornerscorners.lamppost.exception.ServiceActionException;

import us.hornerscorners.lamppost.hl7v3common.constant.ClassCode;
import us.hornerscorners.lamppost.hl7v3common.constant.DeterminerCode;

import us.hornerscorners.lamppost.model.patient.Organization;
import us.hornerscorners.lamppost.model.patient.OtherIdentifier;

import us.hornerscorners.lamppost.transformer.AbstractTransformer;


/**
 *
 * @author jim
 */
@ApplicationScoped
public class OtherIDTransformer
        extends AbstractTransformer<OtherIdentifier, PRPAMT201304UV02OtherIDs> {

    /** Field description */
    @Inject
    private ADTransformer adTransformer;

    /** Field description */
    @Inject
    private IITransformer iiTransformer;

    /** Field description */
    @Inject
    private ONTransformer onTransformer;

    /**
     * Constructs ...
     *
     */
    public OtherIDTransformer() {
        super();
    }

    /**
     * Method description
     *
     *
     * @param id
     *
     * @return
     */
    @Override
    public PRPAMT201304UV02OtherIDs transform(OtherIdentifier id)
            throws ServiceActionException {

        PRPAMT201304UV02OtherIDs result = new PRPAMT201304UV02OtherIDs();

        // ClassCode
        if (StringUtils.isNotBlank(id.getTypeCode())) {

            result.getClassCode().add(id.getTypeCode());
        }

        // ID
        result.getId().addAll(this.iiTransformer.transform(id.getIdentifiers()));

        // Scoping Organization
        COCTMT150002UV01Organization prpaOrg = new COCTMT150002UV01Organization();

        prpaOrg.setClassCode(ClassCode.Organization.getCode());
        prpaOrg.setDeterminerCode(DeterminerCode.Instance.getCode());

        Organization org = id.getScopingOrganization();

        // Scoping Organization Name
        if (StringUtils.isNotBlank(org.getName())) {
            prpaOrg.getName().add(this.onTransformer.transform(org.getName()));
        }

        // Scoping Organization ids
        prpaOrg.getId().addAll(this.iiTransformer.transform(org.getIdentifiers()));

        // Scoping Organization Address
        prpaOrg.getAddr().addAll(this.adTransformer.transform(org.getPostalAddresses()));

        result.setScopingOrganization(prpaOrg);

        return result;
    }
}
